package xyz.chaobei.thread.wait;

import lombok.SneakyThrows;

import java.util.Queue;

/**
 * @copyright (C), 2015-2020
 * @fileName: ProductionThread
 * @author: MRC
 * @date: 2021/1/29 23:26
 * @description:
 */
public class ProductionThread implements Runnable {
    /**
     * 队列大小默认为16
     */
    private Queue list;

    private int num = 0;

    public ProductionThread(Queue queue) {
        this.list = queue;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (list) {
            while (true) {
                if (list.size() >= 3) {
                    System.out.println(Thread.currentThread().getName() + "通知消费者消费产品");
                    list.notifyAll();
                    System.out.println(Thread.currentThread().getName() + "通知完毕");
                    list.wait();
                    System.out.println(Thread.currentThread().getName() + "等待结束。。。");
                } else {
                    list.add("Object num=" + num);
                    Thread.sleep(500L);
                    System.out.println(Thread.currentThread().getName() + "生产者生产产品 num=" + num);
                    num++;
                }
            }
        }
    }
}
